package com.pri.chat.fragment;

import android.content.Intent;
import android.graphics.Outline;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.netease.nim.uikit.business.session.viewholder.GlideBlurTransformation;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.StatusBarUtil;
import com.netease.nim.uikit.net.entity.BannerBean;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.UserBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.activity.AddTxActivity;
import com.pri.chat.activity.HeiMingDanMemberActivity;
import com.pri.chat.activity.InviteActivity;
import com.pri.chat.activity.LookMemberActivity;
import com.pri.chat.activity.MyFriendsActivity;
import com.pri.chat.activity.MyInfoActivity;
import com.pri.chat.activity.PeopleMsgActivity;
import com.pri.chat.activity.RechargeListActivity;
import com.pri.chat.activity.SetActivity;
import com.pri.chat.activity.VipCenterActivity;
import com.pri.chat.activity.WalletActivity;
import com.pri.chat.base.TcWebActivity;
import com.pri.chat.model.UserModel;
import com.pri.chat.utils.Des3Util;
import com.pri.chat.utils.GlideImageLoader;
import com.pri.chat.view.ImageViewPlus;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.fragment.FragmentLazy;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/3
 */
public class PersonCenterFragment extends FragmentLazy {
    Unbinder unbinder;
    //    @BindView(R.id.tv_verify_status)
//    TextView tvVerifyStatus;
    @BindView(R.id.user_id)
    TextView user_id;

    @BindView(R.id.tv_set)
    View tvSet;
    @BindView(R.id.iv_logo)
    ImageViewPlus ivLogo;
    //    @BindView(R.id.rl_logo)
//    RelativeLayout rlLogo;
    @BindView(R.id.tv_me_name)
    TextView tvMeName;
    @BindView(R.id.ll_friends)
    TextView llFriends;
    //    @BindView(R.id.tv_hb_num)
//    TextView tvHbNum;
//    @BindView(R.id.rl_recharge_flower)
//    RelativeLayout rlRechargeFlower;
    @BindView(R.id.rl_vip_center)
    View rlVipCenter;
    @BindView(R.id.ll_wallet)
    View ll_wallet;
    @BindView(R.id.ll_chongzhi)
    View ll_chongzhi;

//    @BindView(R.id.rl_me_wish)
//    RelativeLayout rlMeWish;
//    @BindView(R.id.rl_me_gift)
//    RelativeLayout rlMeGift;
//    @BindView(R.id.iv_bg)
//    ImageView ivBg;

    @BindView(R.id.sex_bg)
    View sex_bg;
    @BindView(R.id.sex_logo)
    ImageView sex_logo;
    @BindView(R.id.tv_dt_age)
    TextView tv_dt_age;

    @BindView(R.id.vip_super_bg)
    TextView vip_super_bg;

    @BindView(R.id.tv_vip)
    TextView tvVip;
    //    @BindView(R.id.tv_super_vip)
//    TextView tvSuperVip;
    @BindView(R.id.rl_super_vip)
    View rlSuperVip;
    @BindView(R.id.person_info)
    View person_info;

    @BindView(R.id.ll_lookme)
    View ll_lookme;

    @BindView(R.id.fensiTv)
    TextView fensiTv;
    @BindView(R.id.guanzhuTv)
    TextView guanzhuTv;
    @BindView(R.id.friendTv)
    TextView friendTv;
    @BindView(R.id.detailLinear)
    View detailLinear;

    @BindView(R.id.person_bzhfk)
    View person_bzhfk;

    @BindView(R.id.yqhy_linear)
    View yqhy_linear;

    @BindView(R.id.banner)
    Banner banner;


    @Override
    protected void onVisible() {
        super.onVisible();
        StatusBarUtil.setTranslucentForImageViewInFragment(getActivity(), 0, getView());
        StatusBarUtil.setLightMode(getActivity());
    }

    @Override
    protected View initViews(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        View view = layoutInflater.inflate(R.layout.fragment_person_center_layout, viewGroup, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        if ("女".equals(SharedHelper.readSex(mContext))) {
            ll_chongzhi.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        setUserInfo();
    }

    UserBean userBean;

    private void setUserInfo() {
        SubscriberOnNextListener<BaseBean<UserBean>> onNextListener = baseBean -> {


            initBanner(baseBean.getData().getBannerList());

            userBean = baseBean.getData();
            tvMeName.setText(userBean.getMemberInfo().getNcName());
            Glide.with(mContext).load(userBean.getMemberInfo().getHeadPic()).into(ivLogo);
            llFriends.setText("粉丝" + String.valueOf(userBean.getNumbers().getFenSiNum()) + " | 关注" + String.valueOf(userBean.getNumbers().getGuanZhuNum()) + " | 好友" + String.valueOf(userBean.getNumbers().getFriendNum()));
            fensiTv.setText("粉丝\n" + String.valueOf(userBean.getNumbers().getFenSiNum()));
            guanzhuTv.setText("关注\n" + String.valueOf(userBean.getNumbers().getGuanZhuNum()));
            friendTv.setText("好友\n" + String.valueOf(userBean.getNumbers().getFriendNum()));
            user_id.setText("ID：" + userBean.getMemberInfo().getUserCode());
            SharedHelper.saveKeyValueForString(mContext, "userCode", userBean.getMemberInfo().getUserCode());
            RequestOptions options = new RequestOptions().fitCenter()
                    .transform(new GlideBlurTransformation(mContext));
//            Glide.with(this).load(userBean.getMemberInfo().getHeadPic()).apply(options).into(new SimpleTarget<Drawable>() {
//                @Override
//                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
//                    ivBg.setImageDrawable(resource);
//                }
//            });
//            Glide.with(mContext).load(userBean.getMemberInfo().getHeadPic()).into(ivBg);
//            if (userBean.getMemberInfo().getRzStatus().equals("1")) {
//                tvVerifyStatus.setText("已认证");
//            } else {
//                tvVerifyStatus.setText("未认证");
//            }
            if (userBean.getMemberInfo().getMemberLevel().equals("否")) {
                tvVip.setVisibility(View.GONE);
            } else {
                tvVip.setVisibility(View.VISIBLE);
                tvVip.setText("VIP");
            }
            if (userBean.getMemberInfo().getSuperVip().equals("否")) {
                vip_super_bg.setVisibility(View.GONE);
            } else {
                vip_super_bg.setVisibility(View.VISIBLE);
                vip_super_bg.setText("SVIP");
            }

            tv_dt_age.setText(userBean.getMemberInfo().getSex());
            if ("男".equals(userBean.getMemberInfo().getSex())) {
                sex_bg.setBackgroundResource(R.drawable.btn_sex_man);
                sex_logo.setImageResource(R.mipmap.man);
            } else {
                sex_bg.setBackgroundResource(R.drawable.btn_sex_woman);
                sex_logo.setImageResource(R.mipmap.woman);
            }
        };
        UserModel userModel = new UserModel();
        userModel.setMemberId(SharedHelper.readId(mContext));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(userModel));
        HttpMethods.getInstance().getMemberCenterInfo(new ProgressSubscriber<>(onNextListener, mContext, false), requestBody);
    }

    @OnClick({R.id.yqhy_linear, R.id.person_bzhfk, R.id.ll_chongzhi, R.id.detailLinear, R.id.guanzhuTv, R.id.friendTv, R.id.fensiTv, R.id.ll_wallet, R.id.ll_lookme, R.id.person_info, R.id.tv_set, R.id.rl_super_vip, R.id.rl_me_tx, R.id.ll_friends, R.id.rl_vip_center})
    public void onViewClicked(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.yqhy_linear:
                RxActivityTool.skipActivity(getContext(), InviteActivity.class);
                break;
            case R.id.person_bzhfk:
                Intent intent = new Intent();
                intent.setClass(mContext, TcWebActivity.class);
                intent.putExtra("title", "帮助");
                intent.putExtra("url", HttpMethods.BASE_URL + "p/front_editorHtmlShow.html?code=bangZhuCode");
                startActivity(intent);
                break;
            case R.id.detailLinear:
                bundle.putString("id", SharedHelper.readId(mContext));
                RxActivityTool.skipActivity(mContext, PeopleMsgActivity.class, bundle);
                break;
            case R.id.fensiTv:
                bundle.putInt("type", 0);
                RxActivityTool.skipActivity(mContext, MyFriendsActivity.class, bundle);
                break;
            case R.id.guanzhuTv:
                bundle.putInt("type", 1);
                RxActivityTool.skipActivity(mContext, MyFriendsActivity.class, bundle);
                break;
            case R.id.friendTv:
                bundle.putInt("type", 2);
                RxActivityTool.skipActivity(mContext, MyFriendsActivity.class, bundle);
                break;
            case R.id.ll_chongzhi:
                RxActivityTool.skipActivity(mContext, RechargeListActivity.class);
                break;
            case R.id.tv_set:
                RxActivityTool.skipActivity(mContext, SetActivity.class);
                break;
            case R.id.rl_me_tx:
                RxActivityTool.skipActivity(mContext, AddTxActivity.class);
                break;
            case R.id.rl_logo:
                RxActivityTool.skipActivity(mContext, MyInfoActivity.class);
                break;
            case R.id.ll_friends:
                bundle.putInt("type", 0);
                RxActivityTool.skipActivity(mContext, MyFriendsActivity.class, bundle);
                break;
            case R.id.rl_vip_center:
                RxActivityTool.skipActivity(mContext, VipCenterActivity.class);
                break;
            case R.id.rl_super_vip:
                if (userBean.getMemberInfo().getSuperVip().equals("否")) {
//                    RxActivityTool.skipActivity(mContext, VipCenterActivity.class);
                } else {
                    RxToast.normal("您已经是超级会员了");
                }
                break;
            case R.id.person_info:
                bundle.putString("id", SharedHelper.readId(mContext));
                RxActivityTool.skipActivity(mContext, PeopleMsgActivity.class, bundle);
                break;
            case R.id.ll_lookme:
                bundle.putBoolean("isVip", "是".equals(userBean.getMemberInfo().getMemberLevel()) || "是".equals(userBean.getMemberInfo().getSuperVip()));
                RxActivityTool.skipActivity(mContext, LookMemberActivity.class, bundle);
                break;
            case R.id.ll_wallet:
                RxActivityTool.skipActivity(mContext, WalletActivity.class);
                break;
        }
    }


    private void initBanner(List<BannerBean> bannerList) {
        ArrayList<String> bList = new ArrayList<>();
        for (int i = 0; i < bannerList.size(); i++) {
            bList.add(bannerList.get(i).getBannerPath());
        }
        banner.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 30);
            }
        });
        banner.setClipToOutline(true);

        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {

            }
        });

        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(bList);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.Default);
        //设置标题集合（当banner样式有显示title时）
//        banner.setBannerTitles(titles);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }
}
